package com.somiz.structure;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import jxl.CellView;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

/**
 * 
 * @author kacem
 * @version 1.0
 * 
 */
public class ModelListeStructure extends AbstractTableModel {
	private static final long serialVersionUID = -7433183395314042287L;
	private List<Structure> structures = new ArrayList<Structure>();;
	private final String[] entetes = { "Code", "Tiers", "Nom complet" };

	public ModelListeStructure() {
		super();
	}

	public int getRowCount() {
		return structures.size();
	}

	public int getColumnCount() {
		return entetes.length;
	}

	public String getColumnName(int columnIndex) {
		return entetes[columnIndex];
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return structures.get(rowIndex).getCode();
		case 1:
			if (structures.get(rowIndex).getTier() != null)
				return structures.get(rowIndex).getTier();
			else
				return "";
		case 2:
			if (structures.get(rowIndex).getDesig() != null)
				return structures.get(rowIndex).getDesig();
			else
				return "";
		default:
			return null; // Ne devrait jamais arriver
		}
	}

	public void addObject(Structure structure) {
		structures.add(structure);
		fireTableRowsInserted(structures.size() - 1, structures.size() - 1);
	}

	public void CreerHeaderExcel(WritableSheet sheet, WritableCellFormat times,
			WritableCellFormat timesBoldUnderline) throws WriteException {
		WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
		times = new WritableCellFormat(times10pt);
		times.setWrap(true);
		WritableFont times10ptBoldUnderline = new WritableFont(
				WritableFont.TIMES, 10, WritableFont.BOLD, false,
				UnderlineStyle.SINGLE);
		timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
		timesBoldUnderline.setWrap(true);
		CellView cv = new CellView();
		cv.setFormat(times);
		cv.setFormat(timesBoldUnderline);
		cv.setAutosize(true);
		sheet.addCell(new Label(0, 0, "yesterday", timesBoldUnderline));
	}

	public void addObjectExcel(WritableSheet sheet,
			WritableCellFormat timesBoldUnderline) throws WriteException,
			RowsExceededException {
		sheet.addCell(new Label(0, 3, "cccc", timesBoldUnderline));
	}

	public PdfPTable CreerHeaderPdf(PdfPTable table) {
		Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
		catFont.setColor(BaseColor.RED);
		PdfPCell c1 = new PdfPCell(new Phrase("Code"));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		c1 = new PdfPCell(new Phrase("Tier"));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		c1 = new PdfPCell(new Phrase("Designation"));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		table.setHeaderRows(1);
		return table;
	}

	public PdfPTable addObjectPDF(Structure structure, PdfPTable table)
			throws BadElementException {
		table.addCell(structure.getCode());
		if (structure.getTier() != null)
			table.addCell(structure.getTier());
		else
			table.addCell("/");

		if (structure.getDesig() != null)
			table.addCell(structure.getDesig());
		else
			table.addCell("/");

		return table;
	}

	public void removeObject(int rowIndex) {
		structures.remove(rowIndex);
		fireTableRowsDeleted(rowIndex, rowIndex);
	}

	public void removeAllObjects() {
		if (structures.size() != 0) {
			for (int i = structures.size() - 1; i >= 0; i--) {
				structures.remove(i);
				fireTableRowsDeleted(i, i);
			}
		}
	}

	public boolean isCellEditable(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return false;
		default:
			return false;
		}
	}
}
